package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.CupsMchntInfoMapper;
import cc.rengu.igas.bomp.common.entity.CupsMchntInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/2
 */
public class CupsMchntInfoMapperImpl implements CupsMchntInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertCupsMchntInfo(CupsMchntInfo cupsMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCupsMchntInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, cupsMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记银联商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", cupsMchntInfo.getInstId(), cupsMchntInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateCupsMchntInfoById(CupsMchntInfo cupsMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateCupsMchntInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, cupsMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记银联商户基本信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", cupsMchntInfo.getInstId(), cupsMchntInfo.getMchntNo(), cupsMchntInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public CupsMchntInfo selectCupsMchntInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCupsMchntInfoById");
        CupsMchntInfo cupsMchntInfo = new CupsMchntInfo();
        cupsMchntInfo.setInstId(instId);
        cupsMchntInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, cupsMchntInfo, cupsMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取银联商户基本信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return cupsMchntInfo;
    }

    @Override
    public CupsMchntInfo selectCupsMchntInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCupsMchntInfoByPrimaryKey");
        CupsMchntInfo cupsMchntInfo = new CupsMchntInfo();
        cupsMchntInfo.setInstId(instId);
        cupsMchntInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, cupsMchntInfo, cupsMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取银联商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return cupsMchntInfo;
    }

    @Override
    public CupsMchntInfo selectCupsMchntInfoByCupsMchntNo(String instId, String cupsMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCupsMchntInfoByCupsMchntNo");
        CupsMchntInfo cupsMchntInfo = new CupsMchntInfo();
        cupsMchntInfo.setInstId(instId);
        cupsMchntInfo.setCupsMchntNo(cupsMchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, cupsMchntInfo, cupsMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取银联商户基本信息失败,instId:<{}>,cupsMchntNo:<{}>,iReturnCode:<{}>", instId, cupsMchntNo, iReturnCode);
            return null;
        }
        return cupsMchntInfo;
    }
}
